package cn.cvs.service.impl;

import cn.cvs.dao.SupplierMapper;
import cn.cvs.pojo.Supplier;
import cn.cvs.service.SupplierService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Resource
    SupplierMapper supplierMapper;

    public List<Supplier> querySupplierPage(String supCode, String supName, Integer pageBean, Integer pageSize) {
        return supplierMapper.querySupplierPage(supCode,supName,(pageBean-1)*pageSize,pageSize);
    }

    public int querySupplierCount(String supCode, String supName) {
        return supplierMapper.querySupplierCount(supCode,supName);
    }

    public Supplier queryByIdAndNameSupplier(Integer id) {
        return supplierMapper.queryByIdAndNameSupplier(id);
    }

    public int updateSupplier(Supplier supplier) {
        return supplierMapper.updateSupplier(supplier);
    }

    public int insertSupplier(Supplier supplier) {
        return supplierMapper.insertSupplier(supplier);
    }

    public int queryByIdPayStatus(Integer supId) {
        return supplierMapper.queryByIdPayStatus(supId);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int deleteSupplier(Integer id, HttpServletRequest request) {
        //根据id查找供货商信息
        Supplier supplier = supplierMapper.queryByIdAndNameSupplier(id);
        //删除供货信息
        int deleteSupplier = supplierMapper.deleteSupplier(id);
        if (!StringUtils.isEmpty(supplier.getCompanyLicPic())) {
            String companyLicPic = request.getSession().getServletContext().getRealPath(supplier.getCompanyLicPic());
            File delCcompanyLicPic = new File(companyLicPic);
            if (delCcompanyLicPic.exists()) {
                delCcompanyLicPic.delete();
            }
        }
        if (!StringUtils.isEmpty(supplier.getOrgCodePic())) {
            String orgCodePic = request.getSession().getServletContext().getRealPath(supplier.getOrgCodePic());
            File delOrgCodePic = new File(orgCodePic);
            if (delOrgCodePic.exists()) {
                delOrgCodePic.delete();
            }
        }
        return deleteSupplier;
    }
}
